/** 盘基本信息 */
const https = require('https');

let options = {
	hostname: 'pan.baidu.com',
	port: 443,
	path: '/',
	method: 'GET',
	headers: {
		'User-Agent': 'pan.baidu.com'
	}
};

/** 获取用户信息 */
function info(cb){
	options.path = "/rest/2.0/xpan/nas?method=uinfo&access_token=" + user.access_token;
	let req = https.request(options, (res)=>{
		let ret = Buffer.allocUnsafe(0)
		res.on('data', (chunk) => {
			ret = Buffer.concat([ret, chunk], ret.length + chunk.length);
		});
		res.on('end', () => {
			//console.log("ret", ret.toString());
			let inf = JSON.parse(ret.toString());
			if(list.errno){
				console.log("info", ret.toString());
				return cb(list.errno);
			}
			cb(0,inf);
		});
	});
	req.end();
}

/** 获取网盘容量信息 */
function quota(cb){
	options.path = "/api/quota?access_token=" + user.access_token;
	let req = https.request(options, (res)=>{
		let ret = Buffer.allocUnsafe(0)
		res.on('data', (chunk) => {
			ret = Buffer.concat([ret, chunk], ret.length + chunk.length);
		});
		res.on('end', () => {
			//console.log("ret", ret.toString());
			let inf = JSON.parse(ret.toString());
			if(list.errno){
				console.log("quota", ret.toString());
				return cb(list.errno);
			}
			cb(0,inf);
		});
	});
	req.end();
}

/** 显示目录信息 */
function list(path, cb){
	options.path = "/rest/2.0/xpan/file?method=list&dir=" + encodeURI(path||'/') + "&access_token=" + user.access_token;
	let req = https.request(options, (res)=>{
		let ret = Buffer.allocUnsafe(0)
		res.on('data', (chunk) => {
			ret = Buffer.concat([ret, chunk], ret.length + chunk.length);
		});
		res.on('end', () => {
			//console.log("ret", ret.toString());
			list = JSON.parse(ret.toString());
			if(list.errno){
				console.log("list", ret.toString());
				return cb(list.errno);
			}
			list = list.list;
			for(let p of list){
				console.log((p.isdir?'D':'F') + ' ' + p.server_filename + ' ' + p.size);
			}
			cb(0, list);
		});
	});
	req.end();
}

/** 获取文件列表全部 */
function listall(spath, cb){
	let cursor = 0, flist = [];
	let fun = function(){
		options.path = "/rest/2.0/xpan/multimedia?method=listall&path=" + encodeURI(spath||'/') + "&start=" + cursor + "&recursion=1&access_token=" + user.access_token;
		let req = https.request(options, (res)=>{
			let ret = Buffer.allocUnsafe(0)
			res.on('data', (chunk) => {
				ret = Buffer.concat([ret, chunk], ret.length + chunk.length);
			});
			res.on('end', () => {
				list = JSON.parse(ret.toString());
				//for(let p of list.list){
				//	console.log((p.isdir?'D':'F') + ' ' + p.path + ' ' + p.size);
				//}
				if(list.errno){
					console.log("listall", spath, list);
					cb(list.errno);
				}else{
					flist = flist.concat(list.list);
					if(list.has_more){
						cursor = list.cursor;
						fun();
					}else{
						cb(0,flist);
					}
				}
				//console.log("ret", ret.toString());
			});
		});
		req.end();
	}
	fun();
}

exports.info = info;
exports.list = list;
exports.listall = listall;